<?php
// 公共函数


/**
 * @brief get_param 获取数据
 * @param $param 变量
 * @return mixed
 * @version v1.0
 * @date 2012-03-23
 */
function get_param($param){
	$val = '';
	if( isset($_POST[$param]) ){
		$val = $_POST[$param];
	} else if( isset($_GET[$param]) ) {
		$val = $_GET[$param];
	}
	if( !get_magic_quotes_gpc() ){
		$val = addslashes($val);
	}
	return trim($val);
}

/**
 * @brief jump_to 调整 
 * @param $url 目标url
 * @return void
 * @version v1.0
 * @date 2012-03-23
 */
function jump_to($url){
	header('Location:'.$url);
}


/**
 * @brief lang 获取语言
 * @param $param 语言变量
 * @return string
 * @version v1.0
 * @date 2012-03-23
 */
function lang($param){
	$lang = include_once(__LIB__."lang/zh-cn.php");
	if( is_array($param) ){
		$str = "";
		foreach($param as $v){
			if(isset($lang)){
				$str .= $lang[$v];
			}else{
				$str .= $v;
			}
		}
		return $str;
	}elseif( isset($lang[$param]) ){
		return $lang[$param];
	}else{
		return $param;
	} 
}

/**
 * @brief get_data 获取表单数据
 * @param $table 数据库表名
 * @return array
 * @version v1.0
 * @date 2012-03-23
 */
function get_data($table=""){
	$data = array();
	if( empty($table) ){
		$arrPost = array_keys($_POST);
		foreach( $arrPost as $v ){
			$data[$v] = get_param($v);	
		}
	}else{
		if( file_exists(__TABLE__.$table.'.php') ){
			$table = include_once(__TABLE__.$table.'.php');
			if( is_array($table) ){
				foreach( $table as $k=>$v ){
					$data[$k] = get_param($v);
				}
			}
		} else {
			$model = new Model($table);
			$fields = $model->get_columns();
			foreach( $fields as $v ){
				$data[$v] = get_param($v);
			}
		}	
	}	
	return $data;
}

/**
 * @brief dump 友好输出变量
 * @param $var 变量
 * @return 
 * @version v1.0
 * @date 2012-03-23
 */
function dump($var){
	ob_start();
	var_dump($var);
	$str = ob_get_clean();
	$str = preg_replace("/\]\=\>\n(\s+)/m","] => ",$str);
	echo "<pre>".htmlspecialchars($str,ENT_QUOTES)."</pre>";
}

/**
 * @brief write_file 写文件
 * @param $file 文件名
 * @param $content 文件内容
 * @param $override 是否允许覆盖
 * @return int
 * @version v1.0
 * @date 2012-03-25
 */
function write_file($file, $content, $override="true"){
	// 无文件名
	if( empty($file) ) return -1;	
	// 不允许覆盖
	if( file_exists($file) && $override ) return -2;	
	
	return @file_put_contents($file, $content);
}

/**
 * @brief ajax_return ajax 返回数据, json格式
 * @param array $array 返回数据数组
 * @return 
 * @version v1.0
 * @date 2012-03-25
 */
function ajax_return($array){
	echo json_encode($array);
	exit;
}


/**
 * @brief load_class 加载对象
 * @param $className
 * @return 
 * @version v1.0
 * @date 2012-03-31
 */
function load_class($className){
	if( file_exists(__LIB__.$className.'.class.php') ){
		require_once(__LIB__.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function load_model($className){
	$className = $className.'Model';
	if( file_exists(__MODEL__.$className.'.class.php') ){
		require_once(__MODEL__.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function load_controller($controller){
	if( !empty($controller) ){
		$arr = explode('.',$controller);
	}else{
		return false;
	}
	$className = $arr[count($arr)-1].'Controller';
	unset($arr[count($arr)-1]);
	$path = "";
	if( is_array($arr) && count($arr)>0 ){
		foreach($arr as $v){
			$path .= $v.'/';
		}
	}
	if( file_exists(__CONTROLLER__.$path.$className.'.class.php') ){
		require_once(__CONTROLLER__.$path.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function valid_str_len($str, $max, $min=0){
	if( function_exists('mb_strlen') ){
		if( mb_strlen($str) > $max || mb_strlen($str) < $min ){
			return false;
		}
	}else{
		if( strlen($str) > $max || strlen($str) < $min ){
			return false;
		}
	}
	return true;
}

function build_tree( $cates, &$parent, &$return ){
	if( !is_array($cates) ) return false;
	foreach( $cates as $k=>$v ){
		$return[] = $v;
		if( isset($parent[$v['id']]) ){
			build_tree( $parent[$v['id']],$parent, $return );
		}
		unset($parent[$v['id']]);
	}
}

function tree($arr){
	$arr_parents = array();
	$new_arr = array();
	foreach( $arr as $v ){
		$arr_parents[$v['pid']][] = array(
						'id'	=> $v['id'],
						'name'	=> $v['name'],
						'pid'	=> $v['pid']
						);
	}	
	ksort($arr_parents);
	$keys = array_keys($arr_parents);
	foreach( $arr_parents as $key=>$val ){
		$level = array_search($key,$keys);
		//$str = str_repeat($seperator,$level);
		foreach( $val as $k=>$v ){
			$arr_parents[$key][$k]['level'] = $level;
		}
	}
	$tmp = array_slice($arr_parents,0,1);
	$return = array();
	build_tree( $tmp[0], $arr_parents, $return);
	return $return;
}

function msg( $msg,$url="" ){
	if( $url==-1 ){
		$url = "";
	}elseif( !empty($url) ){
		$url = 'location.href="'.$url.'";';
	}else{
		$url = 'history.back();';
	}
	echo '<script type="text/javascript">alert("'.$msg.'");'.$url.'</script>';
}
